package hu.androidportal.rss;

import java.text.MessageFormat;

/**
 * @author Gabe
 * 
 * Replace youtube blocks to mobile friendly version:

<object width="640" height="385">
<param name="movie" value="http://www.youtube.com/v/suBlaE2TAj0?fs=1&amp;hl=en_US" />
<param name="allowFullScreen" value="true" /><param name="allowscriptaccess" value="always" />
<embed type="application/x-shockwave-flash" width="640" height="385" 
src="http://www.youtube.com/v/suBlaE2TAj0?fs=1&amp;hl=en_US" allowfullscreen="true" allowscriptaccess="always">
</embed>
</object>

<iframe title="YouTube video player" class="youtube-player" type="text/html" width="600" height="390" 
src="http://www.youtube.com/embed/JIXxXFbrmKA" frameborder="0" allowFullScreen></iframe>

<iframe class="youtube-player" 
type="text/html" width="320" height="240" 
src="http://www.youtube.com/embed/suBlaE2TAj0" frameborder="0"/>
</iframe>
http://img.youtube.com/vi/suBlaE2TAj0/default.jpg
 *
 */
public class YoutubeFilter implements ContentFilter
{
	private static final String OBJECT_START = "<object ";
	private static final String OBJECT_FINISH = "</object>";
	private static final String IFRAME_START = "<iframe ";
	private static final String IFRAME_FINISH = "</iframe>";
	private static final String OBJECT_YOUTUBE_URL = "www.youtube.com/v/";
	private static final String IFRAME_YOUTUBE_URL = "www.youtube.com/embed/";

	private static final String TEMPLATE = "<a href=\"http://www.youtube.com/watch?v={0}\">"
			+ "<img src=\"file:///android_asset/youtube_layer.png\" style=\"width:320px; height:240px; margin:0; padding:0; background-size: cover; background:transparent url(http://img.youtube.com/vi/{0}/default.jpg) no-repeat center\"/>"
			+ "</a>";

	@Override
	public StringBuilder filter( final StringBuilder content )
	{
		replacePattern(content, OBJECT_START, OBJECT_FINISH, OBJECT_YOUTUBE_URL);
		replacePattern(content, IFRAME_START, IFRAME_FINISH, IFRAME_YOUTUBE_URL);
		return content;
	}

	private void replacePattern( final StringBuilder content, final String startTag, final String endTag,
			final String youtubeUrlPattern )
	{
		int start = content.indexOf(startTag);

		while ( start >= 0 )
		{
			//Log.d(Codes.TAG, "Object tag found at " + start);
			int youtubeLink = content.indexOf(youtubeUrlPattern, start + 1);
			final int stop = content.indexOf(endTag, start + 1);
			if ( youtubeLink > 0 && stop > 0 && stop > youtubeLink )
			{
				//valid block found
				String videoId = null;
				youtubeLink += youtubeUrlPattern.length();
				int linkEnd = youtubeLink;
				char c = content.charAt(linkEnd);
				while ( ( c >= 'A' && c <= 'Z' ) || ( c >= 'a' && c <= 'z' ) || ( c >= '0' && c <= '9' ) || c == '-'
						|| c == '_' )
				{
					linkEnd++;
					c = content.charAt(linkEnd);
				}
				videoId = content.substring(youtubeLink, linkEnd);

				content.delete(start, stop);
				final String template = MessageFormat.format(TEMPLATE, videoId);
				content.insert(start, template);
				//Log.d(Codes.TAG, "Video found and replaced: " + template);
			}

			start = content.indexOf(startTag, stop);
		}
	}
}
